#include <stdio.h>      /* for printf() and fprintf() */
#include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */
#include <arpa/inet.h>  /* for sockaddr_in and inet_addr() */
#include <stdlib.h>     /* for atoi() and exit() */
#include <string.h>     /* for memset() */
#include <unistd.h>     /* for close() */

#define ECHOMAX 255     /* Longest string to echo */

void DieWithError(const char *errorMessage) /* External error handling function */
{
    perror(errorMessage);
    exit(1);
}

/* STRUCT Defined in Project Description*/
struct request
{
    char client_ip[16]; /* To hold client IP address in dotted decimal */
    int inc;     /* Incarnation number of client */
    int client;  /* Client number */
    int req;     /* Request number */
    char c;      /* Random character client sends to server */
};
/***************************************/

int main(int argc, char *argv[])
{
    int sock;                        /* Socket descriptor */
    struct sockaddr_in echoServAddr; /* Echo server address */
    struct sockaddr_in fromAddr;     /* Source address of echo */
    unsigned short echoServPort;     /* Echo server port */
    unsigned int fromSize;           /* In-out of address size for recvfrom() */
    char *servIP;                    /* IP address of server */
//  char *echoString;                /* String to send to echo server */
    char echoBuffer[5];      /* Buffer for receiving echoed string */
//    int echoStringLen;               /* Length of string to echo */
    int sendBuffLen = 23;
      int respStringLen;               /* Length of received response */
     char sendBuffer[sendBuffLen];
    /****Test params*****/
    struct request arequest;
    struct request *requestptr;
    /********************/
    int i;

    if (argc != 4)   /* Test for correct number of arguments */
    {
        fprintf(stderr,"Usage: %s <Server IP> <Port#> <Client#>\n", argv[0]);
        exit(1);
    }
                 printf("THIS WAS HIT FIRST\n");
		printf("THIS WAS HIT SECOND\n");

/****** INITIALIZING STRUCT *************/

   
for (i=0;i<=15;i++)
{
  arequest.client_ip[i] = 'd';
}
// *arequest.client_ip = argv[1];           /* First arg: server IP address (dotted quad) */  
    arequest.client = argv[3]; 
   arequest.client = 2;
    arequest.inc = 0;
    arequest.req = 1;
    arequest.c = 'A';

/****************************************/
   printf("THIS IS THE THIRD\n");

 //choString = argv[2];       /* Second arg: string to echo */

   //f ((echoStringLen = strlen(echoString)) > ECHOMAX)  /* Check input length */
    //  DieWithError("Echo word too long");

   //f (argc == 4)
        echoServPort = 7;  /* Use given port, if any */
   // else
     //   echoServPort = 7;  /* 7 is the well-known port for the echo service */
    /* Create a datagram/UDP socket */
    if ((sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
        DieWithError("socket() failed");
    /* Construct the server address structure */
    memset(&echoServAddr, 0, sizeof(echoServAddr));    /* Zero out structure */
    echoServAddr.sin_family = AF_INET;                 /* Internet addr family */
printf("THIS IS THE FOURTH\n");
    echoServAddr.sin_addr.s_addr = inet_addr(servIP);  /* Server IP address */
printf("THIS IS THE FOURTH\n");
    echoServAddr.sin_port   = htons(echoServPort);     /* Server port */

    for( i=0; i<=15; i++)
	{
		sendBuffer[i] = arequest.client_ip[i];
        }
	
     
     for (i=16; i<23; i++)
	{
	 	sendBuffer[i] = 'F';
        }

                  printf("THIS WAS HIT\n");

/*************** START SENDING REQUESTS *********************/

   // while (arequest.req < 21)
		
	         

 		   /* Send the string to the server */
   		 if (sendto(sock, *sendBuffer, sendBuffLen, 0, (struct sockaddr *)
        	       &echoServAddr, sizeof(echoServAddr)) != 1)
       		 DieWithError("sendto() sent a different number of bytes than expected");
  
		 /* Recv a response */
    	/*	fromSize = sizeof(fromAddr);
    		if ((respStringLen = recvfrom(sock, echoBuffer, 5, 0, 
         	(struct sockaddr *) &fromAddr, &fromSize)) != 5)
       	 	DieWithError("recvfrom() failed");

   	 	if (echoServAddr.sin_addr.s_addr != fromAddr.sin_addr.s_addr)
    		{
        		fprintf(stderr,"Error: received a packet from unknown source.\n");
        		exit(1);
    		}

  		//   null-terminate the received data 
   		 echoBuffer[respStringLen] = '\0';
   		 printf("Received:%s\n", echoBuffer);    // Print the echoed arg 
       */

                  printf("%s\n", sendBuffer);
  		  close(sock);
   		 exit(0);

	
}
